package logic

import (
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"goods-sys/goodsrpc/goodsrpc"
	"goods-sys/goodsrpc/internal/svc"
	"goods-sys/models/mysql"
)

type AddPingLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddPingLogic {
	return &AddPingLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *AddPingLogic) AddPing(in *goodsrpc.AddRequest) (*goodsrpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	data := mysql.User{
		Title: in.Title,
		Post:  in.Post,
		Shu:   in.Shu,
		Pole:  in.Pole,
	}
	mysql.DB.Where("title = ?", in.Title).Debug().Create(&data)
	if data.ID != 0 {
		return nil, errors.New("商品同名,请查询添加")
	}

	if err := mysql.DB.Debug().Create(&data).Error; err != nil {
		return nil, nil
	}

	return &goodsrpc.AddResponse{
		UserId: int64(data.ID),
	}, nil
}
